package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.SellService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sell")
public class SellController {
    @Autowired
    private SellService sellService;

    //分页查询和模糊查询
    @RequestMapping("/sell-list")
    @ResponseBody
    public Map<String,Object> allSell(String sellName, String sellType, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Sell> sellList = sellService.allSell(sellName,sellType,empName);
        map.put("total",obj.getTotal());
        map.put("rows",sellList);
        return map;
    }

    //按id查询（详情查询）
    @RequestMapping("/sell-xqlist")
    @ResponseBody
    public Sell xqSell(Integer sellId){
        return sellService.xqSell(sellId);
    }

    //确认按钮（新增和修改）
    @RequestMapping("/add-sell")
    @ResponseBody
    public void addSell(@RequestBody Sell sell){
        sellService.addSell(sell);
    }

    //逻辑删除
    @RequestMapping("/delete-sell")
    @ResponseBody
    public void deleteSell(int id){
        sellService.deleteSell(id);
    }

    //统计图销售费用查询
    @RequestMapping("/sell-lxchaxu")
    @ResponseBody
    public Map<String,Object> sellleixing(String vv,HttpSession session){
        Map<String,Object> map = null;
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        if(vv.equals("费用类别数量统计")) {
            map = sellService.sellleixing(empName);
        }else if(vv.equals("费用类别金额统计")){
            map = sellService.kehufyjine(empName);
        }else if(vv.equals("客户费用前三(已同意)")){
            map = sellService.kehukhfy(empName);
        }else{
            map = sellService.kehufuzeren(empName);
        }
        return map;
    }
}
